package com.teaphy.coroutinecrawler.flow.operators

import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import java.io.IOException

fun main() = runBlocking {

    flow{
        for (num in 0..10) {
           emit(num)
        }
    }.map {

        println("发出的数据：$it")
        if (it > 5) {
            throw TooLargeException("数值太大了")
        }

        it
    }.catch {
        exception ->

        if (exception !is IOException) {
            println("1- 捕获的异常是: $exception")
            emit(-1)
        } else {
            throw exception
        }
    }.catch {
        exception ->
        println("2 - 捕获的异常是: $exception")
    }.collect {
        println("接收到的数据是：$it")
    }

    println("Done")
}

class TooLargeException(msg: String) : Throwable(msg)